package upnp.toy;

import java.beans.PropertyChangeEvent;
import java.util.Dictionary;

import org.osgi.service.upnp.UPnPAction;
import org.osgi.service.upnp.UPnPStateVariable;

public class SetVariableAction implements UPnPAction{

	
	final private String NAME = "SetVariableAction";
	final private String NEW_RESULT_VALUE= "Result";
	final private String NEW_VALUE = "NewVariableValue";
	
	final private String[] IN_ARG_NAMES = new String[]{NEW_VALUE};
	final private String[] OUT_ARG_NAMES = new String[]{NEW_RESULT_VALUE};
	private UPnPStateVariable sampleVariable;
	
	public SetVariableAction(UPnPStateVariable sampleVariable) {
		this.sampleVariable=sampleVariable;
	}

	
	public String[] getInputArgumentNames() {
		// TODO Auto-generated method stub
		return IN_ARG_NAMES;
	}

	
	public String getName() {
		// TODO Auto-generated method stub
		return NAME;
	}

	
	public String[] getOutputArgumentNames() {
		// TODO Auto-generated method stub
		return OUT_ARG_NAMES;
	}

	
	public String getReturnArgumentName() {
		// TODO Auto-generated method stub
		return NEW_RESULT_VALUE;
	}

	
	public UPnPStateVariable getStateVariable(String arg0) {
		// TODO Auto-generated method stub
		return sampleVariable;
	}

	
	public Dictionary invoke(Dictionary args) throws Exception {
		
		String newValue = (String) args.get(NEW_VALUE);
		String oldValue=(String)((SampleVariable)sampleVariable).getCurrentValue();
		((SampleVariable)sampleVariable).setCurrentValue(newValue);
		ToyDevice.notifier.propertyChange(new PropertyChangeEvent(sampleVariable, "SampleVariable", oldValue, newValue));
		args.remove(NEW_VALUE);
		args.put(NEW_RESULT_VALUE,((SampleVariable) sampleVariable).getCurrentValue());
		return args;
	}

}
